package com.yuanfengPlus.basicInfoManage.loginAndAuthentication.service.impl;

import com.yuanfengPlus.basicInfoManage.loginAndAuthentication.dao.UserMapper;
import com.yuanfengPlus.basicInfoManage.loginAndAuthentication.model.LoginUser;
import com.yuanfengPlus.basicInfoManage.loginAndAuthentication.model.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 根据用户名查询用户信息
        User user = userMapper.selectUserByUsername(username);

        // 判断是否查到用户，如果没有查到则抛出异常
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("用户不存在");
        }

        // 返回用户信息
        // TODO 查询权限信息封装

        return new LoginUser(user);
    }
}
